package com.example.order_product.Service.impl;

import com.example.order_product.Entity.Order;
import com.example.order_product.Entity.Product;
import com.example.order_product.Mapper.OrderMapper;
import com.example.order_product.Mapper.ProductMapper;
import com.example.order_product.Service.ProductService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceimpl implements ProductService {
    private final OrderMapper orderMapper;

    private final ProductMapper productMapper;

    public ProductServiceimpl(OrderMapper orderMapper, ProductMapper productMapper) {
        this.orderMapper = orderMapper;
        this.productMapper = productMapper;
    }

    @Override
    public Product findProductByProductId(int productId) {
        Product product = productMapper.selectById(productId);
        return product;
    }

    @Override
    public Product findProductByOrder(Order order) {
        int productId = order.getProductId();
        Product product = findProductByProductId(productId);
        if (product == null) return null;
        return product;
    }

    @Override
    public Product findProductByName(String name) {
        Product product = productMapper.selectByName(name);
        return product;
    }

    @Override
    public void addProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void deleteProduct(int id) {
        productMapper.deleteById(id);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public List<Product> getAllProduct() {
        return productMapper.selectList(null);
    }

    @Override
    public List<Order> findOrderByProductId(int id) {
        List<Order> orderList = orderMapper.selectByProductId(id);
        return orderList;
    }
}
